﻿var link = {
    Current: null,
    getSelectids: function () {
        var result = [];
        var items = link.Current.closest("[panelid]").find("[name='ids']:checked");
        items.each(function () {
            var val = $(this).val();
            result.push(val);
        });
        return result;
    },
    getSelectid: function () {
        var result = null;
        var items = link.Current.closest("[panelid]").find("[name='ids']:checked");
        if (items.size() > 0) {
            result = items.eq(0).val();
        }
        return result;
    },
    onlySelectOne: function () {
        var size = link.Current.closest("[panelid]").find("[name='ids']:checked").size();
        if (size == 0) {
            App.getAlertManager().alert("请选择一项");
            return true;
        } else if (size > 1) {
            App.getAlertManager().alert("只能选择一项");
            return true;
        }
        return false;
    }
};
(function ($) {
    $.fn.link = function (option) {
        option = $.extend({
            application: null
            //text: null,
            //closeable: false,
            //href:null,
            //postdata: {}
        }, option);
        return this.each(function () {
            var view = $(this);
            var target = view.attr("target");
            view.click(function () {
                link.Current = $(this);
                var beforechk=eval("_chk="+view.attr("beforechk"));
                if(beforechk){
                    return false;
                }
                if (target == "navtab") {
                    var t = view.text();
                    var settings = $.extend({
                        text: view.attr("tip") ||view.attr("title") || view.text(),
                        href: view.attr("href"),
                        closeable: view.attr("closeable"),
                        postdata: eval("_o="+view.attr("postdata"))
                    }, option);

                    settings.application.getTabPanelContainer().addTab({ url: settings.href, postdata: settings.postdata, name: settings.text, closeable: settings.closeable });
                    return false;
                } else if (target == "dialog") {
                    var t = view.text();
                    var settings = $.extend({
                        title: view.attr("tip") ||view.attr("title") ||view.text(),
                        content: view.attr("href"),
                        mask: view.attr("mask"),
                        resizeable: view.attr("resizeable"),
                        dragable: view.attr("dragable"),
                        maxable: view.attr("maxable"),
                        minable: view.attr("minable"),
                        width: view.attr("width") || "80%",
                        height: view.attr("height") || "80%",
                        postdata: eval("_o="+view.attr("postdata")),
                        confirm: view.attr("confirm")
                    }, option);
                    if(view.attr("refreshonclose")){
                        settings.onclose=function(){
                            var panelid= link.Current.PanelParent().attr("panelid");
                            App.getTabPanelContainer().refreshTab(panelid);
                        };
                    };
                    if (settings.postdata) {
                        settings.content = { url: settings.content, postdata: settings.postdata };
                    }
                    if (settings.confirm) {
                        settings.application.getAlertManager().confirm(settings.confirm, function () {
                            settings.application.getDialogManager().open(settings);
                        });
                    } else {
                        settings.application.getDialogManager().open(settings);
                    }
                    return false;
                } else if (target == "ajax") {
                    var t = view.attr("tip") ||view.attr("title") ||view.text();
                    var settings = $.extend({
                        title: view.text(),
                        url: view.attr("href"),
                        data: eval("_o="+view.attr("postdata")),
                        type: view.attr("type"),
                        backtype: view.attr("backtype"),
                        mask: view.attr("mask"),
                        successmsg: view.attr("successmsg") || "操作成功",
                        confirm: view.attr("confirm"),
                        fill: view.attr("fill"),
                        tabid: view.PanelParent().helper("remotePanel").getId()
                    }, option);
                    if (settings.confirm) {
                        settings.application.getAlertManager().confirm(settings.confirm, function () {
                            settings.application.getAjaxManager().ajaxpost(settings);
                        });
                    } else {
                        settings.application.getAjaxManager().ajaxpost(settings);
                    }
                    return false;
                }
            });
        });
    }
})(jQuery);